# import requests
import base64
from bs4 import BeautifulSoup
# import assert
def testPage(id):
    url = 'http://www.tjsql.com/data.aspx?d=%s'%str(id)

    with open('/home/ider/Documents/pages/%s.html'%id,'rt')as f:
        res = f.read()
    # soup = BeautifulSoup(res.text, "html5lib")
    soup = BeautifulSoup(res, "lxml")
    # soup = BeautifulSoup(res.text)

    ##第一段内容
    tables = soup.find('textarea',id='yysy').string
    tables = base64.b64decode(tables).decode('utf8')

    sdemo = soup.find('textarea',id='sdemo').string
    nj = soup.find('input',id='nj')['value']
    stitl = soup.find('input',id='stitl')['value']

    tableName = soup.find('div',class_='einfo').h2.string

    # print('表名 : %s'%tableName)
    # print('数据 : %s'%tables)
    # print('来源 : %s'%nj)
    # print('标题 : %s'%stitl)
    # print('标注 : %s'%sdemo)

    ##第三段内容
    elites = []
    ttgls = []
    for elite in soup.find_all('li',class_='elite'):
        elites.append(elite.a.string)
    for ttgl in soup.find_all('span',class_='ttgl'):
        ttgls.append(ttgl.a.string)

    # print('本栏目数据 : %s'%elites)
    # print('历年相似数据 : %s'%ttgls)

    ###第二段内容
    try:
        for tr in soup.find('table',class_='bbtable').find_all('tr'):
            # name =
            td = tr.find_all('td')
            name = td[0].string.replace('：','').strip()
            if '标签' in name:
                continue
            value = td[1].string.strip()

            # print('%s:%s'%(name,value))

            if '标题' in name:
                assert stitl in value

            if '来源' in name:
                assert nj in value
    except AttributeError as e:
        pass

    assert stitl is not None
    assert len(tables) > 20
    assert nj is not None
    # assert sdemo is not None
    # assert len(elites) >0
    # assert len(ttgls) >0
    # assert elites not None


def main():
    for i in range(83406,171613):
        try:
            testPage(i)
        except Exception as e:
            print(i)
            raise

if __name__=='__main__':
    main()
